package com.neusoft.ysu.populationmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.neusoft.ysu.populationmanagement.constant.TabelDic;
import com.neusoft.ysu.populationmanagement.entity.CommonResult;
import com.neusoft.ysu.populationmanagement.entity.OperationLog;
import com.neusoft.ysu.populationmanagement.entity.Operator;
import com.neusoft.ysu.populationmanagement.entity.Population;
import com.neusoft.ysu.populationmanagement.mapper.OperationLogMapper;
import com.neusoft.ysu.populationmanagement.mapper.OperatorMapper;
import com.neusoft.ysu.populationmanagement.service.IOperatorService;
import com.neusoft.ysu.populationmanagement.service.IPopulationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 作者：赵子鉴
 * @since 2024-06-18
 */
@Service
public class OperatorServiceImpl implements IOperatorService {

    @Autowired
    private OperatorMapper operatorMapper;


    /**
     * @param operator
     * @return CommonResult：501，1
     * @description 公安民警注册完成
     * @author 张幸晔
     * @date 2024-06-21 15:34
     */
    @Override
    public CommonResult registration(Operator operator) {
        //首先判断idNum是否冲突
        CommonResult selectIdNum = selectOperatorByIdNum(operator.getIdnum());
        if (selectIdNum.getCode() != 0) {
            return new CommonResult<>(501, "同一身份证号人重复注册", null);
        }
        //根据usernaem判读是否冲突
        CommonResult selectUsername = selectOperatorByUsername(operator.getUsername());
        if (selectUsername.getCode() != 0) {
            return new CommonResult(501, "账号重复注册", null);
        }
        operator.setIsDelete(TabelDic.IS_DELETE_NO);
        operator.setCreateTime(LocalDateTime.now());
        //非同一人注册非同一账号
        int rows = operatorMapper.insert(operator);
        if (rows <= 0) {
            return new CommonResult<>(501, "注册失败，请联系管理员", null);
        }
        return new CommonResult<>(1, "注册成功", null);
    }

    /**
     * @param idNum
     * @return CommonResult：0，1
     * @description 根据身份证号检索警员信息
     * @author 张幸晔
     * @date 2024-06-21 15:28
     */

    @Override
    public CommonResult selectOperatorByIdNum(String idNum) {
        LambdaQueryWrapper<Operator> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Operator::getIdnum, idNum).eq(Operator::getIsDelete, TabelDic.IS_DELETE_NO);
        List<Operator> operators = operatorMapper.selectList(lambdaQueryWrapper);
        if (operators.size() <= 0) {
            return new CommonResult<>(0, "为检索到相关警员", null);
        }
        return new CommonResult<>(1, "警员信息", operators.get(0));
    }

    /**
     * @param username
     * @return CommonResult：0，1
     * @description 根据username检索警员信息
     * @author 张幸晔
     * @date 2024-06-21 15:29
     */

    @Override
    public CommonResult selectOperatorByUsername(String username) {
        LambdaQueryWrapper<Operator> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Operator::getUsername, username).eq(Operator::getIsDelete, TabelDic.IS_DELETE_NO);
        List<Operator> operators = operatorMapper.selectList(lambdaQueryWrapper);
        if (operators.size() <= 0) {
            return new CommonResult<>(0, "为检索到相关警员", null);
        }
        return new CommonResult<>(1, "警员信息", operators.get(0));
    }

    /**
     * @param pid
     * @return
     * @description 根据派出所id删除
     * @author 张幸晔
     * @date 2024-06-21 16:18
     */

    @Override
    public CommonResult deleteByPid(Long pid) {
        LambdaUpdateWrapper<Operator> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Operator::getPid, pid);
        Operator operator = new Operator();
        operator.setIsDelete(TabelDic.IS_DELETE_YES);
        int rows = operatorMapper.update(operator, lambdaUpdateWrapper);
        if (rows <= 0) {
            return new CommonResult<>(0, "警员删除失败", null);
        }
        return new CommonResult<>(1, "警员删除成功", null);
    }

    /**
     * @param operator
     * @return
     * @description 根据ID删除
     * @author 张幸晔
     * @date 2024-06-21 16:18
     */

    @Override
    public CommonResult deleteOperatorById(Operator operator) {
        operator.setIsDelete(TabelDic.IS_DELETE_YES);
        int rows = operatorMapper.updateById(operator);
        if (rows <= 0) {
            return new CommonResult<>(0, "警员删除失败", null);
        }
        return new CommonResult<>(1, "警员删除成功", null);
    }

    /**
     * @param operator
     * @return
     * @description 启用警察
     * @author 张幸晔
     * @date 2024-06-21 16:18
     */

    @Override
    public CommonResult enableOperator(Operator operator) {
        operator.setIsDelete(TabelDic.IS_DELETE_NO);
        int rows = operatorMapper.updateById(operator);
        if (rows <= 0) {
            return new CommonResult<>(0, "警员启用失败", null);
        }
        return new CommonResult<>(1, "警员启用成功", null);
    }

    @Override
    public List<Operator> selectAll() {
        return operatorMapper.selectList(new LambdaQueryWrapper<Operator>());
    }

    @Override
    public CommonResult changeLevel(Operator operator) {
        int rows = operatorMapper.updateById(operator);
        if (rows<=0){
            return new CommonResult<>(0,"变更等级失败",null);
        }
        return new CommonResult<>(1,"变更等级成功",operator);
    }


}
